package com.trace.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trace.app.entity.Comment;
import com.trace.app.entity.ResponseResult;
import com.trace.app.entity.User;
import com.trace.app.entity.consts.AppHttpCodeEnum;
import com.trace.app.entity.vo.CommentVo;
import com.trace.app.entity.vo.PageVo;
import com.trace.app.mapper.CommentMapper;
import com.trace.app.service.CommentService;
import com.trace.app.service.UserService;
import com.trace.app.util.BeanCopyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (Comment)表服务实现类
 *
 * @author lionkliu
 * @since 2024-04-21 13:27:06
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private UserService userService;

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        Page<Comment> page = page(new Page<>(pageNum, pageSize), queryWrapper);
        PageVo<Comment> pageVo = new PageVo<>();
        pageVo.setResult(page.getRecords());
        pageVo.setTotal(page.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(Comment comment) {
        return save(comment) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(Comment comment) {
        LambdaUpdateWrapper<Comment> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(Comment::getId, comment.getId());
        // TODO
        update(null, updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult listByProId(Integer proId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Comment::getCreateTime);
        queryWrapper.eq(Comment::getProId, proId);
        List<Comment> list = list(queryWrapper);
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        List<CommentVo> collect = commentVos.stream().map(commentVo -> {
            LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.eq(User::getId, commentVo.getUserId());
            User user = userService.getOne(userWrapper);
            commentVo.setAvatar(user.getAvatar());
            commentVo.setUsername(user.getUsername());
            commentVo.setNickName(user.getNickName());
            return commentVo;
        }).collect(Collectors.toList());

        return ResponseResult.okResult(collect);
    }

    @Override
    public ResponseResult search(Integer pageNum, Integer pageSize, Integer userId, Integer proId, String content) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        if (!Objects.isNull(content)) {
            queryWrapper.like(Comment::getContent, content);
        }
        if (!Objects.isNull(proId)) {
            queryWrapper.eq(Comment::getProId, proId);
        }
        if (!Objects.isNull(userId)) {
            queryWrapper.eq(Comment::getUserId, userId);
        }
        Page<Comment> userPage = new Page<>(pageNum, pageSize);
        page(userPage, queryWrapper);
        PageVo<Comment> userPageVo = new PageVo<>();
        userPageVo.setTotal(userPage.getTotal());
        userPageVo.setPageTotal(userPage.getPages());
        userPageVo.setResult(userPage.getRecords());

        return ResponseResult.okResult(userPageVo);
    }

}


